package com.glory.brepda.activity.mfg;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.brepda.R;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.TinLayerThicknessCheckEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class TinLayerThicknessCheckActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.etLotId)
    FreeUI_ClearEditText etLotId;
    @BindView(R.id.etEqpId)
    FreeUI_ClearEditText etEqpId;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.teMatType)
    TextView teMatType;
    @BindView(R.id.teProductModel)
    TextView teProductModel;
    @BindView(R.id.teLimit)
    TextView teLimit;
    @BindView(R.id.teDocType)
    TextView teDocType;
    @BindView(R.id.teCarRule)
    TextView teCarRule;
    @BindView(R.id.etnum)
    EditText etnum;
    @BindView(R.id.flReasonList)
    FrameLayout flReasonList;
    @BindView(R.id.tvReason)
    TextView tvReason;
    @BindView(R.id.ivReasonArrow)
    ImageView ivReasonArrow;
    @BindView(R.id.unqualifiedReasonLinear)
    LinearLayout unqualifiedReasonLinear;
    @BindView(R.id.unqualifiedReason)
    FreeUI_ClearEditText unqualifiedReason;

    List<EditText> points = new ArrayList<>();
    private List<String> Qualified =new ArrayList<>(Arrays.asList("合格","不合格"));
    private FreeUI_GeneralPop myPop;

    private  String POINT = "";
    private List<String> POINTS = new ArrayList<>(Arrays.asList("","","","","",""));
    private List<String> POINTS2 = new ArrayList<>(Arrays.asList("","","","","","","","","","","",""));
    private List<String> POINTS3 = new ArrayList<>(Arrays.asList("","","","","","","","","","","","","","",""));
    private List<String> POINTS4 = new ArrayList<>(Arrays.asList("","","","","","","","","",""));
    Double minLimit = 0.0;
    Double maxLimit = 0.0;
    String docType = "";
    String carRule = "";

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_tin_layer_thickness_check;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        tvBaseSubmit.setText("提交");
        tvReason.setText("合格");

    }

    @Override
    protected void initData() {
        initEdit(etLotId, this::getDetail);
        unqualifiedReasonLinear.setVisibility(View.GONE);
    }

    @OnClick({R.id.ivshow,R.id.tvBaseSubmit,R.id.flReasonList})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ivshow:
                if(carRule.equals("是")){
                    if (docType.equals("JGGC") || docType.equals("ZCGC")){
                        add3();
                    }else{
                        add2();
                    }
                }else{
                    if (docType.equals("JGGC") || docType.equals("ZCGC")){
                        add4();
                    }else{
                        add1();
                    }
                }

                break;
            case R.id.tvBaseSubmit:
                submit();

                break;
            case R.id.flReasonList:
                if (myPop != null) {
                    if (myPop.isShowing()) {
                        myPop.dismiss();
                    }
                } else {
                    myPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flReasonList.getWidth(), Qualified, ivReasonArrow, (codeS, i) -> {
                        tvReason.setText(codeS);
                        if (tvReason.getText().toString().equals("不合格")) {
                            unqualifiedReasonLinear.setVisibility(View.VISIBLE);
                        } else {
                            unqualifiedReasonLinear.setVisibility(View.GONE);
                        }
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivReasonArrow);
                myPop.showAsDropDown(flReasonList, 0, 0);

        }
    }
    private void submit(){
        if(getText(etLotId).equals("")){
            showToast("批次号未录入", false);
            return;
        }
        if(getText(etEqpId).equals("")){
            showToast("设备号未录入", false);
            return;
        }
        if(tvReason.getText().toString()=="不合格" && getText(unqualifiedReason).equals("")){
            showToast("请输入外观检测不合格原因",false);
            return;
        }

        String failList = "";
        String emptyList = "";
        String dataList = "";


        for(EditText ed :points){
            if (ed.getText().toString().equals("")){
                emptyList += ed.getHint().toString() + " ";
            }else if (Double.parseDouble(ed.getText().toString())<=minLimit || Double.parseDouble(ed.getText().toString())>=maxLimit){
                failList += ed.getHint().toString() + " ";
            }
            dataList += ed.getText() + ";";
        }
        if (!failList.equals("") && !emptyList.equals("")){
            showToast("点位" + emptyList + "未测量 \n" + "点位" + failList +"不合格", false);
            return;
        }else if(!failList.equals("")){
            showToast("点位" + failList +"不合格", false);
            return;
        }else if(!emptyList.equals("")){
            showToast("点位" + emptyList + "未测量", false);
            return;
        }

        FreeUI_EntityCallBack<BaseResponseBean<TinLayerThicknessCheckEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<TinLayerThicknessCheckEntity>>
                (new TypeToken<BaseResponseBean<TinLayerThicknessCheckEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<TinLayerThicknessCheckEntity>> response) {
                super.onSuccess(response);
                if(response.body().getResponse().getBody().getMainMatType().equals("rewrite")){
//                    ShowDialog.showDialog(TinLayerThicknessCheckActivity.this,"已覆盖记录","提示");
                    AlertDialog dialog=null;
                    dialog = new AlertDialog.Builder(TinLayerThicknessCheckActivity.this).setTitle("提示")
                            .setMessage("提交成功，原记录已覆盖")
                            .setIcon(R.drawable.lanjian)
                            .setPositiveButton("确定",new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    finish();
                                }
                            })
                            .create();
                    dialog.show();
                }else{
                    showToast("提交成功",true);
                    finish();
                }

            }

            @Override
            public void onError(Response<BaseResponseBean<TinLayerThicknessCheckEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "TinLayerThicknessCheck");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE","SAVE");
        map.addProperty("DATALIST",dataList);
        map.addProperty("LOTID",getText(etLotId));
        map.addProperty("EQUIPMENTID",getText(etEqpId));
        map.addProperty("PRODUCTMODEL",teProductModel.getText().toString());
        map.addProperty("MAINMATTYPE",teMatType.getText().toString());
        map.addProperty("STANDARD",teLimit.getText().toString());
        map.addProperty("VISUALINSPECTION",tvReason.getText().toString());
        if(tvReason.getText().toString().equals("不合格")){
            map.addProperty("UNQUALIFIEDREASON",getText(unqualifiedReason));
        }else{
            map.addProperty("UNQUALIFIEDREASON","");
        }

        NetUtils.requestPostNet("IF.TINLAYERTHICKNESSCHECK", "Common", TinLayerThicknessCheckActivity.this, map, callBack);
    }

    private void getDetail(String lotId) {
        FreeUI_EntityCallBack<BaseResponseBean<TinLayerThicknessCheckEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<TinLayerThicknessCheckEntity>>
                (new TypeToken<BaseResponseBean<TinLayerThicknessCheckEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<TinLayerThicknessCheckEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(TinLayerThicknessCheckActivity.this,false,getSupportFragmentManager())){
                    TinLayerThicknessCheckEntity body = response.body().getResponse().getBody();
                    teProductModel.setText(body.getProductModel());
                    teMatType.setText(body.getMainMatType());
                    minLimit=0.0;
                    maxLimit=0.0;
                    try {
                        minLimit = Double.parseDouble(body.getMinLimit());
                        maxLimit = Double.parseDouble(body.getMaxLimit());

                    }catch (Exception e){
                        showToast("封装" + body.getMainMatType() + "未录入锡层厚度标准",false);
                    }
                    String limit = minLimit + "-" +  maxLimit + "um";
                    teLimit.setText(limit);
                    carRule = body.getCarRule().equals("Y")?"是":"否";
                    docType = body.getDocType();
                    teCarRule.setText(carRule);
                    teDocType.setText(docType);
                    if(body.getPoint1()!=null){
                        etEqpId.setText(body.getEquipmentId());
                        if(carRule.equals("是")){
                            if (docType.equals("JGGC") || docType.equals("ZCGC")){
                                POINTS3.clear();
                                POINTS3.add(body.getPoint1());
                                POINTS3.add(body.getPoint2());
                                POINTS3.add(body.getPoint3());
                                POINTS3.add(body.getPoint4());
                                POINTS3.add(body.getPoint5());
                                POINTS3.add(body.getPoint6());
                                POINTS3.add(body.getPoint7());
                                POINTS3.add(body.getPoint8());
                                POINTS3.add(body.getPoint9());
                                POINTS3.add(body.getPoint10());
                                POINTS3.add(body.getPoint11());
                                POINTS3.add(body.getPoint12());
                                POINTS3.add(body.getPoint13());
                                POINTS3.add(body.getPoint14());
                                POINTS3.add(body.getPoint15());
                            }else{
                                POINTS2.clear();
                                POINTS2.add(body.getPoint1());
                                POINTS2.add(body.getPoint2());
                                POINTS2.add(body.getPoint3());
                                POINTS2.add(body.getPoint4());
                                POINTS2.add(body.getPoint5());
                                POINTS2.add(body.getPoint6());
                                POINTS2.add(body.getPoint7());
                                POINTS2.add(body.getPoint8());
                                POINTS2.add(body.getPoint9());
                                POINTS2.add(body.getPoint10());
                                POINTS2.add(body.getPoint11());
                                POINTS2.add(body.getPoint12());
                            }
                        }else{
                            if (docType.equals("JGGC") || docType.equals("ZCGC")){
                                POINTS4.clear();
                                POINTS4.add(body.getPoint1());
                                POINTS4.add(body.getPoint2());
                                POINTS4.add(body.getPoint3());
                                POINTS4.add(body.getPoint4());
                                POINTS4.add(body.getPoint5());
                                POINTS4.add(body.getPoint6());
                                POINTS4.add(body.getPoint7());
                                POINTS4.add(body.getPoint8());
                                POINTS4.add(body.getPoint9());
                                POINTS4.add(body.getPoint10());
                            }else{
                                POINTS.clear();
                                POINTS.add(body.getPoint1());
                                POINTS.add(body.getPoint2());
                                POINTS.add(body.getPoint3());
                                POINTS.add(body.getPoint4());
                                POINTS.add(body.getPoint5());
                                POINTS.add(body.getPoint6());
                            }
                        }
                    }
                }

            }

            @Override
            public void onError(Response<BaseResponseBean<TinLayerThicknessCheckEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "TinLayerThicknessCheck");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("LOTID",lotId);
        map.addProperty("ACTIONTYPE","GETDETAIL");
        NetUtils.requestPostNet("IF.TINLAYERTHICKNESSCHECK","Common", TinLayerThicknessCheckActivity.this,map,callBack);
    }

    private void add1() {
        AlertDialog.Builder builder = new AlertDialog.Builder(TinLayerThicknessCheckActivity.this);
        builder.setTitle("请输入厚度");
        //  加载一个xml的布局文件作为一个View对象
        View view = LayoutInflater.from(TinLayerThicknessCheckActivity.this).inflate(R.layout.point_data_input1, null);
        builder.setView(view);
        final EditText point1 = view.findViewById(R.id.point1);
        final EditText point2 = view.findViewById(R.id.point2);
        final EditText point3 = view.findViewById(R.id.point3);
        final EditText point4 = view.findViewById(R.id.point4);
        final EditText point5 = view.findViewById(R.id.point5);
        final EditText point6 = view.findViewById(R.id.point6);

        points.clear();
        points.add(point1);
        points.add(point2);
        points.add(point3);
        points.add(point4);
        points.add(point5);
        points.add(point6);
        if (!POINTS.isEmpty()){
            for (int i = 0; i < 6; i++) {
                points.get(i).setText(POINTS.get(i));
            }
        }

//        设置取消键
       
//       设置确定键
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                POINT="";

                for(EditText ed :points){
                    POINT += (ed.getHint().toString()+":"+ed.getText()+"、");
                }

                for (int j = 0; j < 6; j++) {
                    POINTS.set(j,points.get(j).getText().toString());
                }
                etnum.setText(POINT);
            }
        });
        builder.show();
    }

    private void add2() {
        AlertDialog.Builder builder = new AlertDialog.Builder(TinLayerThicknessCheckActivity.this);
        builder.setTitle("请输入厚度");
        //  加载一个xml的布局文件作为一个View对象
        View view = LayoutInflater.from(TinLayerThicknessCheckActivity.this).inflate(R.layout.point_data_input2, null);
        builder.setView(view);
        final EditText point1 = view.findViewById(R.id.point1);
        final EditText point2 = view.findViewById(R.id.point2);
        final EditText point3 = view.findViewById(R.id.point3);
        final EditText point4 = view.findViewById(R.id.point4);
        final EditText point5 = view.findViewById(R.id.point5);
        final EditText point6 = view.findViewById(R.id.point6);
        final EditText point7 = view.findViewById(R.id.point7);
        final EditText point8 = view.findViewById(R.id.point8);
        final EditText point9 = view.findViewById(R.id.point9);
        final EditText point10 = view.findViewById(R.id.point10);
        final EditText point11 = view.findViewById(R.id.point11);
        final EditText point12 = view.findViewById(R.id.point12);

        points.clear();
        points.add(point1);
        points.add(point2);
        points.add(point3);
        points.add(point4);
        points.add(point5);
        points.add(point6);
        points.add(point7);
        points.add(point8);
        points.add(point9);
        points.add(point10);
        points.add(point11);
        points.add(point12);

        if (!POINTS2.isEmpty()){
            for (int i = 0; i < 12; i++) {
                points.get(i).setText(POINTS2.get(i));
            }
        }

//        设置取消键
       
//       设置确定键
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                POINT="";

                for(EditText ed :points){
                    POINT += (ed.getHint().toString()+":"+ed.getText()+"、");
                }

                for (int j = 0; j < 12; j++) {
                    POINTS2.set(j,points.get(j).getText().toString());
                }
                etnum.setText(POINT);
            }
        });
        builder.show();
    }

    private void add3() {
        AlertDialog.Builder builder = new AlertDialog.Builder(TinLayerThicknessCheckActivity.this);
        builder.setTitle("请输入厚度");
        //  加载一个xml的布局文件作为一个View对象
        View view = LayoutInflater.from(TinLayerThicknessCheckActivity.this).inflate(R.layout.point_data_input3, null);
        builder.setView(view);
        final EditText point1 = view.findViewById(R.id.point1);
        final EditText point2 = view.findViewById(R.id.point2);
        final EditText point3 = view.findViewById(R.id.point3);
        final EditText point4 = view.findViewById(R.id.point4);
        final EditText point5 = view.findViewById(R.id.point5);
        final EditText point6 = view.findViewById(R.id.point6);
        final EditText point7 = view.findViewById(R.id.point7);
        final EditText point8 = view.findViewById(R.id.point8);
        final EditText point9 = view.findViewById(R.id.point9);
        final EditText point10 = view.findViewById(R.id.point10);
        final EditText point11 = view.findViewById(R.id.point11);
        final EditText point12 = view.findViewById(R.id.point12);
        final EditText point13 = view.findViewById(R.id.point13);
        final EditText point14 = view.findViewById(R.id.point14);
        final EditText point15 = view.findViewById(R.id.point15);

        points.clear();
        points.add(point1);
        points.add(point2);
        points.add(point3);
        points.add(point4);
        points.add(point5);
        points.add(point6);
        points.add(point7);
        points.add(point8);
        points.add(point9);
        points.add(point10);
        points.add(point11);
        points.add(point12);
        points.add(point13);
        points.add(point14);
        points.add(point15);

        if (!POINTS3.isEmpty()){
            for (int i = 0; i < 15; i++) {
                points.get(i).setText(POINTS3.get(i));
            }
        }

//        设置取消键
       
//       设置确定键
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                POINT="";

                for(EditText ed :points){
                    POINT += (ed.getHint().toString()+":"+ed.getText()+"、");
                }

                for (int j = 0; j < 15; j++) {
                    POINTS3.set(j,points.get(j).getText().toString());
                }
                etnum.setText(POINT);
            }
        });
        builder.show();
    }

    private void add4() {
        AlertDialog.Builder builder = new AlertDialog.Builder(TinLayerThicknessCheckActivity.this);
        builder.setTitle("请输入厚度");
        //  加载一个xml的布局文件作为一个View对象
        View view = LayoutInflater.from(TinLayerThicknessCheckActivity.this).inflate(R.layout.point_data_input4, null);
        builder.setView(view);
        final EditText point1 = view.findViewById(R.id.point1);
        final EditText point2 = view.findViewById(R.id.point2);
        final EditText point3 = view.findViewById(R.id.point3);
        final EditText point4 = view.findViewById(R.id.point4);
        final EditText point5 = view.findViewById(R.id.point5);
        final EditText point6 = view.findViewById(R.id.point6);
        final EditText point7 = view.findViewById(R.id.point7);
        final EditText point8 = view.findViewById(R.id.point8);
        final EditText point9 = view.findViewById(R.id.point9);
        final EditText point10 = view.findViewById(R.id.point10);


        points.clear();
        points.add(point1);
        points.add(point2);
        points.add(point3);
        points.add(point4);
        points.add(point5);
        points.add(point6);
        points.add(point7);
        points.add(point8);
        points.add(point9);
        points.add(point10);


        if (!POINTS4.isEmpty()){
            for (int i = 0; i < 10; i++) {
                points.get(i).setText(POINTS4.get(i));
            }
        }

//        设置取消键
       
//       设置确定键
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                POINT="";

                for(EditText ed :points){
                    POINT += (ed.getHint().toString()+":"+ed.getText()+"、");
                }

                for (int j = 0; j < 10; j++) {
                    POINTS4.set(j,points.get(j).getText().toString());
                }
                etnum.setText(POINT);
            }
        });
        builder.show();
    }


}
